#include "StdAfx.h"
#include "CircleBuilder.h"
#include <BRepBuilderAPI_MakeEdge.hxx> 
#include <gp_Circ.hxx>
#include "LabelTools.h"
#include "VarList.h"
#include "ShapeInfoFactory.h"

REGISTERE_SHAPEINFO(CCircleInfo, CIRCLE_CP_GUID);
//////////////////////////////////////////////////////////////////////////
CCircleInfo::CCircleInfo()
:dRadius(0),
dirNormal(0,0,1)
{
	m_szGUID = CIRCLE_CP_GUID;
}
CCircleInfo::~CCircleInfo()
{
}
void CCircleInfo::Reset()
{
	dirNormal = gp_Dir(0,0,1);
	dRadius = 0;
}
void CCircleInfo::GetParams(CVarList& varList, UINT nID)
{
	varList.SetID(CIRCLE_CP_GUID);
	varList.SetValue(CENTER_POINT, ptCenter);
	varList.SetValue(DIRECTION, dirNormal);
	varList.SetValue(RADIUS, dRadius);
}
void CCircleInfo::SetParams(const CVarList& varList, UINT nID)
{
	CPointProxy point;
	varList.GetValue(CENTER_POINT, point);
	ptCenter = point;

	varList.GetValue(DIRECTION, point);
	dirNormal = point;

	varList.GetValue(RADIUS, dRadius);

}
bool CCircleInfo::GetFromLabel(TDF_Label& label)
{
	TDF_Label childLabel;

	if(!LabelTools::GetChild(label, CENTER_POINT, ptCenter, childLabel))
		return false;

	if(!LabelTools::GetChild(label, DIRECTION, dirNormal, childLabel))
		return false;

	if(!LabelTools::GetChild(label, RADIUS, dRadius, childLabel))
		return false;

	return true;
}

void CCircleInfo::AddToLabel(TDF_Label& label)
{
	LabelTools::SetChild(label, CENTER_POINT, ptCenter);
	LabelTools::SetChild(label, DIRECTION, dirNormal);
	LabelTools::SetChild(label, RADIUS, dRadius);
}

bool CCircleInfo::ModifyLabel(TDF_Label& label, TFunction_Logbook& log)
{
	if(!LabelTools::UpdateChild(label, CENTER_POINT, ptCenter, log))
		return false;

	if(!LabelTools::UpdateChild(label, DIRECTION, dirNormal, log))
		return false;

	if(!LabelTools::UpdateChild(label, RADIUS, dRadius, log))
		return false;

	return true;
}

bool CCircleInfo::IsModified(TDF_Label& label,const TFunction_Logbook& log)
{
	if(log.IsModified(label.FindChild(CENTER_POINT)))
		return true; 

	if(log.IsModified(label.FindChild(DIRECTION)))
		return true; 

	if(log.IsModified(label.FindChild(RADIUS)))
		return true; 

	return false;
}
//////////////////////////////////////////////////////////////////////////

CCircleBuilder::CCircleBuilder(void)
{
}

CCircleBuilder::~CCircleBuilder(void)
{
}

bool CCircleBuilder::Build(const CString& szCmdParam, TopoDS_Shape& shape)
{
	return false;
}

bool CCircleBuilder::Build(CCircleInfo* pShapeInfo, TopoDS_Shape& shape)
{
	ASSERT(pShapeInfo);
	if(!pShapeInfo)
		return false;

	try
	{
		gp_Ax2 aix(pShapeInfo->ptCenter, pShapeInfo->dirNormal);
		gp_Circ circle(aix, pShapeInfo->dRadius);
		BRepBuilderAPI_MakeEdge builder(circle);
		shape = builder.Shape();
	}
	catch (...) 
	{
		return false;
	}

	return true;
}